<?php

/**
 * Albireo Kernel
 *
 * @copyright  Copyright (c) 2010 Albireo Solutions
 * @package    Kernel
 */

namespace KernelModule;

use Nette\Environment;

use Albireo\Mode;
use Albireo\RemoteServer\RemoteCommunication;
use Albireo\RemoteServer\MalformedURLException;

use KernelModule\LogsModule\PhpModule\Display;

/**
 * Sign In Page presenter.
 *
 * @author     Albireo Solutions
 * @package    Kernel
 */
class OverviewPresenter extends BasePresenter {

    private $aclLogs;

    /**
     * @return AclLogs model
     */
    public function getAclLogs() {
        if ($this->aclLogs === NULL)
            $this->aclLogs = new LogsModule\AclModule\AclLogs();
        return $this->aclLogs;
    }

    /**
     * Render default action
     */
    public function renderDefault() {
        $this->template->title = _("Overview");
        if (Mode::isCritical()) {
            $this->template->critical = Mode::getMessage();
//            $module = "config management";
//            $this->template->dbname = $module->getActive("database");
//            if ($this->template->dbname) {
//                $db = $module->getConfig($this->template->dbname);
//                $this->template->dbserver = $db->common->database["host"];
//                $database_url = $db->common->database["host"];
//                $remote_server = new RemoteCommunication("http://$database_url/");
//                try {
//                    $response = $remote_server -> getHeaders();
//                    if ( $response == FALSE ) {
//                        $this->template->additional_info = _("Database server sends no response.");
//                    } else {
//                        $this->template->additional_info = _("Database server is responding.");
//                    }
//                } catch (MalformedURLException $e) {
//                    $this->template->additional_info = _("URL to connect remote server is malformed.");
//                }
//            } else {
//                $this->template->dbserver = _("No database has been set active");
//                $this->template->additional_info = _("Set up a connection to RDBMS.");
//            }

        } else {
            /*
             * Shows Php logs
             */
            $this->template->arePhpLogsAllowed = $this->getUser()->isAllowed("Kernel:Logs:Php:Process", "default");
            if($this->template->arePhpLogsAllowed) {
                $model = new Display();
                $model->initializeLogFile("clean");
                $this->template->logArray = $model->handleLogs(5, 1, false);
                $this->template->isUnprocessed = (file_exists(\APP_DIR."/log/php_error.log"))? TRUE : FALSE;
            }
            
            
            /*
             * Shows Acl logs
             */
            $this->template->areAclLogsAllowed = $this->getUser()->isAllowed("Kernel:Logs:Acl:Display", "default");

            $logs = array();
            if($this->template->areAclLogsAllowed) {
                $dSource = $this->getAclLogs()->getDataSource();
                $dSource->applyLimit(5);
                

                foreach ($dSource as $row) {
                    if($row->acl_id == NULL) {
                        $row->acl_id = _("N/A");
                    }
                    $timestamp = $row->time->getTimestamp();
                    $timeStr = date("j.n.Y H:i:s", $timestamp);
                    $row->time = $timeStr;
                    $logs[] = $row;
                }
            }
            $this->template->logs = $logs;
        }
        $this->showMenu();
    }
}